package com.zengqingfa.juc.n3;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zengqingfa
 * @className TestJoin
 * @description
 * @create 2021/6/7 22:24
 */
@Slf4j(topic = "c.TestJoin3")
public class TestJoin3 {
    static int r1 = 0;

    public static void main(String[] args) throws Exception {
//        test2();
        test3();
    }

    /**
     * 22:43:21:660 [main] c.TestJoin3 - join begin
     * 22:43:23:659 [main] c.TestJoin3 - r1: 10  cost: 2001
     * @throws Exception
     */
    private static void test2() throws Exception {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r1 = 10;
        });
        long start = System.currentTimeMillis();
        t1.start();
        log.debug("join begin");
        t1.join(4000);
        long end = System.currentTimeMillis();
        log.debug("r1: {}  cost: {}", r1, end - start);
    }

    /**
     * 22:44:27:725 [main] c.TestJoin3 - join begin
     * 22:44:29:227 [main] c.TestJoin3 - r1: 0  cost: 1504
     * @throws Exception
     */
    private static void test3() throws Exception {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r1 = 10;
        });
        long start = System.currentTimeMillis();
        t1.start();
        log.debug("join begin");
        t1.join(1500);
        long end = System.currentTimeMillis();
        log.debug("r1: {}  cost: {}", r1, end - start);
    }
}